#-*- coding: UTF-8 -*-
# @Time    : 2019/4/14 13:21
# @Author  : xiongzongyang
# @Site    : 
# @File    : question_template.py
# @Software: PyCharm

'''



9:nnt 参演评分 小于 x
10:nnt 电影类型
11:nnt nnr 合作 电影列表
12:nnt 电影数量
13:nnt 出生日期
'''
from chatbot.query import Query
import re

class QuestionTemplate():
    def __init__(self):
        self.q_template_dict={
            0:self.get_system_infra,
            1:self.get_error_reason,
            2:self.get_error_checkway,
            3:self.get_error_effect,
            4:self.get_system_error,
            5:self.get_error_module,
            6:self.get_module_server,
            7:self.get_error_way,
            8:self.get_module_error,
            9:self.get_error_class,
            10:self.get_sonclass,
            11:self.get_know,
            12:self.get_know_info
        }

        # 连接数据库
        self.graph = Query()
        # 测试数据库是否连接上
        # result=self.graph.run("match (m:Movie)-[]->() where m.title='卧虎藏龙' return m.rating")
        # print(result)
        # exit()

    def get_question_answer(self,question,template):
        # 如果问题模板的格式不正确则结束
        assert len(str(template).strip().split("\t"))==2
        template_id,template_str=int(str(template).strip().split("\t")[0]),str(template).strip().split("\t")[1]
        self.template_id=template_id
        self.template_str2list=str(template_str).split()

        # 预处理问题
        question_word,question_flag=[],[]
        for one in question:
            word, flag = one.split("/")
            question_word.append(str(word).strip())
            question_flag.append(str(flag).strip())
        assert len(question_flag)==len(question_word)
        self.question_word=question_word
        self.question_flag=question_flag
        self.raw_question=question
        # 根据问题模板来做对应的处理，获取答案
        answer=self.q_template_dict[template_id]()
        return answer

    # 获取系统名字
    def get_system_name(self):
        ## 获取sys在原问题中的下标
        tag_index = self.question_flag.index("sys")
        ## 获取系统名称
        system_name = self.question_word[tag_index]
        return system_name
    def get_error_name(self):
        ## 获取nm在原问题中的下标
        tag_index = self.question_flag.index("er")
        ## 获取问题名称
        error_name = self.question_word[tag_index]
        return error_name
    def get_module_name(self):
        ## 获取nm在原问题中的下标
        tag_index = self.question_flag.index("nm")
        ## 获取模块名称
        module_name = self.question_word[tag_index]
        return module_name
    def get_class_name(self):
        ## 获取nm在原问题中的下标
        tag_index = self.question_flag.index("kl")
        ## 获取分类名称
        class_name = self.question_word[tag_index]
        return class_name
    def get_sonclass_name(self):
        ## 获取nm在原问题中的下标
        tag_index = self.question_flag.index("ksl")
        ## 获取子类名称
        sonclass_name = self.question_word[tag_index]
        return sonclass_name
    def get_know_name(self):
        ## 获取nm在原问题中的下标
        tag_index = self.question_flag.index("kn")
        ## 获取子类名称
        know_name = self.question_word[tag_index]
        return know_name
    # 0:sys 系统架构
    def get_system_infra(self):
        # 获取系统名称，这个是在原问题中抽取的
        system_name=self.get_system_name()
        cql = f"MATCH (m:system)-[r:rels_sys_module]->(n:link_module) where m.name =~'(?i).*{system_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set = set(answer)
        answer_list = list(answer_set)
        answer = "、".join(answer_list)
        final_answer = system_name + "由" + str(answer) + "等模块组成！"
        return final_answer
    # 1:er 问题原因
    def get_error_reason(self):
        error_name = self.get_error_name()
        cql = f"match (m:error) where m.name =~'.*(?i){error_name}.*' return m.reason"
        print(cql)
        answer = self.graph.run(cql)
        answer_set = set(answer)
        answer_list = list(answer_set)
        answer = "、".join('%s' %id for id in answer_list)
        final_answer = error_name + "问题可能有以下原因造成：" + str(answer) + "！"
        return final_answer
    # 2:er 问题检查方法
    def get_error_checkway(self):
        error_name = self.get_error_name()
        cql = f"match(m:error) where m.name=~'.*(?i){error_name}.*' return m.check_way"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = error_name + "常用一下检测方法：" + str(answer) + "!"
        return final_answer
    # 3:er 问题造成影响
    def get_error_effect(self):
        error_name = self.get_error_name()
        cql = f"match(m:error) where m.name=~'.*(?i){error_name}' return m.err_effect"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = error_name + "可能会造成如下影响：" + str(answer) + "!"
        return final_answer
    # 4:nm 系统相关的问题
    def get_system_error(self):
        system_name=self.get_system_name()
        cql = f"match (m:system)-[r:rels_sys_error]->(n:error) where m.name =~'.*(?i){system_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set = set(answer)
        answer_list = list(answer_set)
        answer = "、".join(answer_list)
        final_answer = system_name + "常见的问题有" + str(answer) + "等！"
        return final_answer
    # 5:er 问题相关的模块
    def get_error_module(self):
        error_name = self.get_error_name()
        cql = f"match(m:error)-[r:rels_link_module]->(n:link_module) where m.name =~'(?i).*{error_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = "与问题" +error_name + "关连的模块有：" + str(answer) + "!"
        return final_answer
    # 6:nm 模块相关的服务器
    def get_module_server(self):
        module_name = self.get_module_name()
        cql = f"match(m:server)-[r:rels_server_module]->(n:link_module) where n.name =~'(?i).*{module_name}.*' return m.ip_addr"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = module_name + "包含有以下服务器：" + str(answer) + "!"
        return final_answer
    # 7:er 解决问题的方案
    def get_error_way(self):
        error_name = self.get_error_name()
        cql = f"match(m:error) where m.name=~'.*(?i){error_name}.*' return m.solve_way"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = "问题" +error_name + "的解决方案如下：" + str(answer) + "!"
        return final_answer

    # 8:nm 模块相关的问题
    def get_module_error(self):
        module_name = self.get_module_name()
        cql = f"match(m:link_module)-[r:rels_server_module]->(n:server) where m.name =~'(?i).*{module_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = module_name + "包含有以下服务器：" + str(answer) + "!"
        return final_answer
    # 9:er 问题的类别
    def get_error_class(self):
        error_name = self.get_error_name()
        cql = f"match(m:error)-[r:rels_err_class]->(n:err_class) where m.name =~'(?i).*{error_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = "问题" +error_name + "的分类属于" + str(answer) + "!"
        return final_answer
    # 10:kl 类别的子类
    def get_sonclass(self):
        class_name = self.get_class_name()
        cql = f"match(m:err_class)-[r:rels_cla_son_cla]->(n:err_son_class) where m.name =~'(?i).*{class_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = class_name + "的子类有：" + str(answer) + "!"
        return final_answer
    # 11:ksl 知识点
    def get_know(self):
        sonclass_name = self.get_sonclass_name()
        cql = f"match(m:err_son_class)-[r:rels_son_cla_konw]->(n:know) where m.name =~'(?i).*{sonclass_name}.*' return n.name"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = sonclass_name + "的知识点有：" + str(answer) + "!"
        return final_answer
    # 12:kn 知识
    def get_know_info(self):
        know_name = self.get_know_name()
        cql = f"match(m:know) where m.name =~'(?i).*{know_name}.*' return m.know_desc"
        print(cql)
        answer = self.graph.run(cql)
        answer_set=set(answer)
        answer_list=list(answer_set)
        answer="、".join(answer_list)
        final_answer = know_name + "内容为：" + str(answer) + "!"
        return final_answer